<template>
  <div class="loginView">
    
    <div class="login">
      <h1 style="color: rgba(28, 104, 219, 0.904)">Welcome Administrators</h1>
      <el-form
        :label-position="labelPosition"
        label-width="80px"
        :model="user"
        :rules="rules"
        ref="user"
      >
        <el-form-item label="账号" prop="email">
          <el-input v-model="user.email" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="user.password" placeholder="请输入密码"></el-input>
        </el-form-item>
      </el-form>
      <el-row>
        <el-button type="primary" @click="logon('user')">登录</el-button>
      </el-row>
    </div>
    <div class="title">
      <!-- 在css中设置了图片 -->
    </div>
  </div>
</template>
<script>
import { login } from "../api/usrMG";
export default {
  name: "LoginView",
  data() {
    return {
      labelPosition: "right",
      user: {
        email: "", //邮箱
        password: "", //密码
      },
      rules: {
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          { validator: this.validateEmail, trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 8, max: 16, message: '长度在8 到 16 个字符', trigger: 'blur' }
        ],
      },
    };
  },
  methods: {
    logon(formName) {
      //登录事件
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.user)
            .then((res) => {
              //先判断登录是否成功
              if (res.data.code == 200) {
                // console.log(res)
                // console.log(res.data.data.userId);
                // alert("登录成功");
                // //成功把返回的userId保存在前端,1秒钟之后在跳出然后跳转
                this.$message({
                  type:'success',
                  message:"登录成功",
                  showClose:true
                })
                localStorage.setItem("userIdAdmin", res.data.data.userId);
                localStorage.setItem("headAdmin",res.data.data.head)
                // // 跳转到管理员首页
                this.$router.push("/user");
              } else {
                this.$message({
                  type: "error",
                  showClose: true,
                  message: res.data.code + ":" + res.data.msg,
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    if (localStorage.getItem("userIdAdmin") != null) {
      this.$router.push("/user");
    }
  },
};
</script>
<style >
.loginView {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  /* 使 .loginView 占据整个视口高度 */
}

.login {
  width: 500px;
  height: 500px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  /* 其他样式，如背景色、边框等 */
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2); /* 更强烈的凸出效果 */
}

.title {
  background-image: url(../assets/login.jpg);
  width: 400px;
  height: 500px;
  display: flex;
  background-size: cover; /* 这将使背景图像覆盖整个容器，同时保持宽高比 */
  background-position: center; /* 这将确保图像从中心开始放置 */
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2); /* 更强烈的凸出效果 */
}
body {
  margin: 0;
}
</style>
